/*
 * @Author: szx
 * @Date: 2022-03-06 14:25:00
 * @LastEditTime: 2022-03-06 14:47:32
 * @Description:
 * @FilePath: \leetcode\6000-6100\6019\6019.js
 */
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var replaceNonCoprimes = function (nums) {
    let st = [nums[0]];
    for (let i = 1; i < nums.length; i++) {
        st.push(nums[i]);
        while (st.length > 1) {
            const res = gcd(st[st.length - 1], st[st.length - 2]);
            if (res !== 1) {
                st.push(Math.floor((st.pop() * st.pop()) / res));
            } else break;
        }
    }
    return st;
};
function gcd(a, b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
// console.log(gcd(899, 20677));
console.log(replaceNonCoprimes([6, 4, 3, 2, 7, 6, 2]));
console.log(replaceNonCoprimes([2, 2, 1, 1, 3, 3, 3]));
console.log(replaceNonCoprimes([287, 41, 49, 287, 899, 23, 23, 20677, 5, 825]));
